package com.example.hellodemo;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class AlertDialogActivity<UIButton> extends AppCompatActivity {
    private Button typeBtn1, typeBtn2, typeBtn3,typeBtn4, typeBtn5;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alertdialog);
        typeBtn1 = findViewById(R.id.alertdialog_type1);
        typeBtn2 = findViewById(R.id.alertdialog_type2);
        typeBtn3 = findViewById(R.id.alertdialog_type3);
        typeBtn4 = findViewById(R.id.alertdialog_type4);
        typeBtn5 = findViewById(R.id.alertdialog_type5);

        View.OnClickListener listener = new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                switch (v.getId()){

                    case R.id.alertdialog_type1: // 默认提示确认框 通过 Builder模式构建
                        AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);
                        builder.setTitle("温馨提示：")
                                .setIcon(R.drawable.name)
                                .setMessage("最新股票动荡，投资需谨慎")
                                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                                                                @Override
                                                                public void onClick(DialogInterface dialog, int which) {

                                                                }
                                                            })
                                .setNeutralButton("再看看", new DialogInterface.OnClickListener() {
                                                                   @Override
                                                                   public void onClick(DialogInterface dialog, int which) {

                                                                 }
                                                            })
                                .setPositiveButton("没事，搞起", new DialogInterface.OnClickListener() {
                                                                @Override
                                                                public void onClick(DialogInterface dialog, int which) {

                                                                }
                                                            }).show();

                        break;
                    case R.id.alertdialog_type2: //  单选列表
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(AlertDialogActivity.this);

                        final String[] ary = new  String[]{"男","女"};
                        builder1.setTitle("请选择性别").setItems(ary, new DialogInterface.OnClickListener() {


                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(AlertDialogActivity.this, ary[which], Toast.LENGTH_SHORT).show();
                            }
                        }).show();


                        break;
                    case R.id.alertdialog_type3: //默认 单选框
                    final AlertDialog.Builder builder2 =  new AlertDialog.Builder(AlertDialogActivity.this);
                    final String[] ary3 = new String[]{"男","女"};
                    builder2.setTitle("选择性别：").setSingleChoiceItems(ary3,0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(AlertDialogActivity.this, ary3[which], Toast.LENGTH_SHORT).show();
                            dialog.dismiss();

                        }
                    }).setCancelable(false).show();
                        break;
                    case R.id.alertdialog_type4: // 默认多选框
                        AlertDialog.Builder builder4 = new AlertDialog.Builder(AlertDialogActivity.this);
                      final  String[] ary4 = new String[]{"打球","看书","看电影"};
                              boolean[] b = new boolean[]{false,false,false};
                        builder4.setTitle("选择兴趣:").setMultiChoiceItems(ary4, b, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                Toast.makeText(AlertDialogActivity.this, ary4[which] + ":" + isChecked, Toast.LENGTH_SHORT).show();

                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).show();
                        break;
                    case R.id.alertdialog_type5: // 自定义
                        View view = LayoutInflater.from(AlertDialogActivity.this).inflate(R.layout.login_alert,null); // 获取xml布局对象
                        EditText ed1 = view.findViewById(R.id.ed_name);
                        EditText ed2 = view.findViewById(R.id.ed_password);
                        Button btn = view.findViewById(R.id.login_btn2);
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });

                        AlertDialog.Builder builder5 = new AlertDialog.Builder(AlertDialogActivity.this);
                        builder5.setTitle("登录：").setView(view).show();

                        break;

                }
            }
        };
        typeBtn1.setOnClickListener(listener);
        typeBtn2.setOnClickListener(listener);
        typeBtn3.setOnClickListener(listener);
        typeBtn4.setOnClickListener(listener);
        typeBtn5.setOnClickListener(listener);

    }

}
